import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const types = {
  SET_IS_AUTNENTIATED: 'SET_IS_AUTNENTIATED', // 是否认证通过
  SET_USER: 'SET_USER' // 用户信息
}

const state = { // 需要维护的状态
  isAutnenticated: false,  // 是否认证
  userInfo: {}, // 存储token用户信息
  BASEURL: `http://localhost`,
  CuisineModel: [ '重庆小面', '黄焖鸡', '水煮', '南昌拌粉', '宫保鸡丁', '水煮肉片', '酸汤肥牛','过桥米线' ]
}

const getters = {
  isAutnenticated: state => state.isAutnenticated,
  userInfo: state => state.userInfo
}

const mutations = {
  [ types.SET_IS_AUTNENTIATED ] (state, isAutnenticated) {
    if (isAutnenticated)
      state.isAutnenticated = isAutnenticated
    else
      state.isAutnenticated = false
  },
  [ types.SET_USER ] (state, user) {
    if (user)
      state.userInfo = user
    else
      state.userInfo = {}
  },
}

const actions = {
  setIsAutnenticated: ({ commit }, isAutnenticated) => {
    commit(types.SET_IS_AUTNENTIATED, isAutnenticated)
  },
  setUser: ({ commit }, user) => {
    commit(types.SET_USER, user)
  },
  clearCurrentState: ({ commit }) => {
    commit(types.SET_IS_AUTNENTIATED, false)
    commit(types.SET_USER, null)
  },
}

export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions
})
